package cq.zhcqk.activity;

import android.graphics.Bitmap;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.TextView;

import com.dou361.dialogui.DialogUIUtils;

import cq.zhcqk.R;
import cq.zhcqk.base.BaseActivity;
import cq.zhcqk.util.Common;

/**
 * Created by zhangle on 2018/1/9.
 */
public class DianzibaoActivity extends BaseActivity implements View.OnClickListener{

    private String URL = "http://106.14.202.180/blog/open/dzb/296";
    private WebView webTest;
    private TextView header_back_text;

    @Override
    protected void initView() {
        setContentView(R.layout.activity_suzibao);
        webTest = (WebView) findViewById(R.id.webTest);
        header_back_text = (TextView) findViewById(R.id.header_back_text);
        header_back_text.setOnClickListener(this);
        initBack();
        setWeb();
    }

    @Override
    protected void initData() {
        webTest.loadUrl(URL);
    }

    private void initBack(){
        ImageView back = (ImageView) findViewById(R.id.header_back);
        TextView title = (TextView) findViewById(R.id.header_text);
        title.setText("数字报");
        back.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.header_back:
                if (webTest.canGoBack())
                {
                    webTest.goBack(); //goBack()表示返回WebView的上一页面
                }else
                {
                    finish();
                }
                break;
            case R.id.header_back_text:
                finish();
                break;
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK) ) {
            if (webTest.canGoBack())
            {
                webTest.goBack(); //goBack()表示返回WebView的上一页面
                return true;
            }else
            {
                finish();
                return true;
            }

        }
        return false;
    }

    @Override
    public void finish() {
        super.finish();
        overridePendingTransition(R.anim.slide_left_in, R.anim.slide_right_out);
    }

    public void setWeb(){
        WebSettings settings = webTest.getSettings();
        settings.setJavaScriptEnabled(true);
        settings.setBuiltInZoomControls(true);
        settings.setLoadWithOverviewMode(true);
        settings.setUseWideViewPort(true);
        settings.setDatabaseEnabled(true);
        settings.setSupportZoom(true);
//        settings.setDisplayZoomControls(true);

        if(webTest != null)
        {
            webTest.setWebViewClient(new WebViewClient()
            {
                @Override
                public void onPageFinished(WebView view,String url)
                {
                    DialogUIUtils.dismiss(buildBean);
                }

                @Override
                public void onPageStarted(WebView view, String url, Bitmap favicon) {
                    super.onPageStarted(view, url, favicon);
                    buildBean.show();
                }
            });
        }
    }
}
